<!doctype html>
<html lang="en">
<head>
    <title>Code coverage report for pad.js</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="prettify.css" />
    <link rel="stylesheet" href="base.css" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style type='text/css'>
        .coverage-summary .sorter {
            background-image: url(sort-arrow-sprite.png);
        }
    </style>
</head>
<body>
<div class='wrapper'>
  <div class='pad1'>
    <h1>
      <a href="index.html">All files</a> pad.js
    </h1>
    <div class='clearfix'>
      <div class='fl pad1y space-right2'>
        <span class="strong">100% </span>
        <span class="quiet">Statements</span>
        <span class='fraction'>19/19</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">100% </span>
        <span class="quiet">Branches</span>
        <span class='fraction'>12/12</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">100% </span>
        <span class="quiet">Functions</span>
        <span class='fraction'>1/1</span>
      </div>
      <div class='fl pad1y space-right2'>
        <span class="strong">100% </span>
        <span class="quiet">Lines</span>
        <span class='fraction'>16/16</span>
      </div>
    </div>
  </div>
  <div class='status-line high'></div>
<pre><table class="coverage">
<tr><td class="line-count quiet">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42</td><td class="line-coverage quiet"><span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">5x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">4x</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">4x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">4x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">9x</span>
<span class="cline-any cline-yes">9x</span>
<span class="cline-any cline-yes">9x</span>
<span class="cline-any cline-yes">4x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">4x</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-neutral">&nbsp;</span>
<span class="cline-any cline-yes">1x</span></td><td class="text"><pre class="prettyprint lang-js">/**
 * Created by lenovo on 2017/6/13.
 */
/**
 * 高效填充字符函数（填充次数为2的n次方中的n次）
 * @param  {string}  str    待填充的字符串
 * @param  {number}  len    填充后的长度
 * @param  {any}     ch     填充物，可以单个字符，也可以多个字符。
 * @param  {any}     direct 填充位置（r: 右填充，其他：左填充）
 */
function pad(str, len, ch, direct) {
    var left = '', //超出的填充字符
        pad = '', // 完整填充字符
        ch = ch + ''; //填充物
&nbsp;
    //完整填充字符
    len = len - str.length;
&nbsp;
    // 1、填充长度必须大于0，否则返回原字符串，例如（pad('aa', 0, 'tabca')）
    if (!(len &gt;= 0)) return str;
&nbsp;
    if (ch.length &gt; 0 &amp;&amp; len % ch.length !== 0) {
        console.log(222)
        //2、不能正好完整填充，则会有超出的填充字符，例如（pad('aa', 7, 'tabc')）
        left = ch.slice(0, len % ch.length);
    }
&nbsp;
    // 3、获取实际填充长度，如果超过一个字符，填充次数会相应除去,例如（pad('aa', 7, 't')和2）
    len = Math.floor(len / (ch.length));
&nbsp;
    while(true) {
        // 4、长度如果为偶数，则直接对折加如果不是，最后长度为0也会进入该函数，例如（pad('aa', 10, 'taca')和2）
        if (len % 2) pad += ch;// if (len &amp; 1) pad += ch;
        len &gt;&gt;= 1;// len = Math.floor(len / 2);
        if (len) ch += ch;
        else break;
    }
    // 5、默认为左填充，如果想右填充则direct传'r'，例如（pad('aa', 7, 't','r')和2）
    return (direct === 'r' ? str + pad + left : left + pad + str);
}
&nbsp;
module.exports = pad;</pre></td></tr>
</table></pre>
<div class='push'></div><!-- for sticky footer -->
</div><!-- /wrapper -->
<div class='footer quiet pad2 space-top1 center small'>
  Code coverage
  generated by <a href="https://istanbul.js.org/" target="_blank">istanbul</a> at Thu Jun 15 2017 10:28:41 GMT+0800 (中国标准时间)
</div>
</div>
<script src="prettify.js"></script>
<script>
window.onload = function () {
        if (typeof prettyPrint === 'function') {
            prettyPrint();
        }
};
</script>
<script src="sorter.js"></script>
</body>
</html>
